package com.ld.wms.business.material.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.wms.business.material.mapper.MaterialExampleExtMapper;
import com.ld.wms.business.material.mapper.MaterialExampleMapper;
import com.ld.wms.business.material.model.MaterialExampleExtModel;
import com.ld.wms.business.material.model.MaterialExampleModel;
import com.ld.wms.business.material.model.MaterialTypeModel;
import com.ld.wms.business.material.service.MaterialExampleService;
import com.ld.wms.business.material.service.MaterialTypeService;
import com.ld.wms.business.material.vo.req.MaterialExampleExtReqVo;
import com.ld.wms.business.material.vo.req.MaterialExampleReqVo;
import com.ld.wms.business.material.vo.resp.MaterialExampleExtRespVo;
import com.ld.wms.business.material.vo.resp.MaterialExampleInventoryRespVo;
import com.ld.wms.business.material.vo.resp.MaterialExampleLikeSearchRespVo;
import com.ld.wms.business.material.vo.resp.MaterialExampleRespVo;
import com.ld.wms.business.notice.service.WarnConfigService;
import com.ld.wms.business.notice.vo.resp.WarnConfigRespVo;
import com.ld.wms.business.user.model.UserAccountModel;
import com.ld.wms.business.user.service.UserAccountService;
import com.ld.wms.common.vo.GenerateListVo;
import com.ld.wms.common.vo.GeneratePageListVo;
import com.ld.wms.sysenum.DeleteFlagEnum;
import com.ld.wms.sysenum.MaterialStatusEnum;
import com.ld.wms.sysenum.WarnConfigTypeEnum;
import com.ld.wms.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物料实例表，具体的物料想信息 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2025-02-24
 */
@Service
public class MaterialExampleServiceImpl extends ServiceImpl<MaterialExampleMapper, MaterialExampleModel> implements MaterialExampleService {

    @Autowired
    private MaterialExampleMapper materialExampleMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private MaterialExampleExtMapper materialExampleExtMapper;

    @Autowired
    private WarnConfigService warnConfigService;

    @Autowired
    private MaterialTypeService materialTypeService;

    @Autowired
    private UserAccountService userAccountService;

    @Override
    public GeneratePageListVo<MaterialExampleRespVo> getPageByCondition(Long materialTypeId, String casCode, String name, Integer current, Integer size) {
        IPage<MaterialExampleRespVo> iPage = new Page<>(current, size);
        IPage<MaterialExampleRespVo> materialExampleRespVoIPage = materialExampleMapper.selectPageByCondition(iPage, materialTypeId, casCode, name, null);
        Map<Long, MaterialTypeModel> allMap = materialTypeService.getAllMap();
        Map<Long, UserAccountModel> allUserMap = userAccountService.getAllUserMap();
        for (MaterialExampleRespVo data : materialExampleRespVoIPage.getRecords()) {
            data.setMaterialTypeName(allMap.get(data.getMaterialTypeId()).getName());
            data.setCreateByName(allUserMap.get(data.getCreateBy()).getAccountName());
        }

        return new GeneratePageListVo<>(materialExampleRespVoIPage);
    }

    @Override
    public GenerateListVo<MaterialExampleLikeSearchRespVo> getMaterialExampleLikeSearch(String name) {
        List<MaterialExampleLikeSearchRespVo> materialExampleLikeSearchRespVoList = materialExampleMapper.selectByName(name);
        return new GenerateListVo<>(materialExampleLikeSearchRespVoList);
    }

    @Override
    public MaterialExampleRespVo getDetails(Long id) {
        MaterialExampleRespVo materialExampleRespVo = new MaterialExampleRespVo();
        MaterialExampleModel materialExampleModel = materialExampleMapper.selectById(id);
        BeanUtils.copyProperties(materialExampleModel, materialExampleRespVo);

        // 创建人,类型名称
        Map<Long, UserAccountModel> userAccountModelMap = userAccountService.getAllUserMap();
        Map<Long, MaterialTypeModel> materialTypeModelMap = materialTypeService.getAllMap();
        UserAccountModel userAccountModel = userAccountModelMap.get(materialExampleRespVo.getCreateBy());
        MaterialTypeModel materialTypeModel = materialTypeModelMap.get(materialExampleRespVo.getMaterialTypeId());
        materialExampleRespVo.setCreateByName(userAccountModel.getAccountName());
        materialExampleRespVo.setMaterialTypeName(materialTypeModel.getName());

        // 查询物料扩展字段
        List<MaterialExampleExtRespVo> materialExampleExtRespVoList = materialExampleExtMapper.selectListByMaterialExampleId(id);
        materialExampleRespVo.setMaterialExampleExtVoList(materialExampleExtRespVoList);
        return materialExampleRespVo;
    }

    @Override
    public void deleteById(Long id) {
        MaterialExampleModel materialExampleModel = new MaterialExampleModel();
        materialExampleModel.setId(id);
        materialExampleModel.setDelFlag(DeleteFlagEnum.YES.getCode());
        materialExampleMapper.updateById(materialExampleModel);
    }

    @Override
    public void updateById(MaterialExampleReqVo reqVo) {
        MaterialExampleModel materialExampleModel = new MaterialExampleModel();
        BeanUtils.copyProperties(reqVo, materialExampleModel);
        materialExampleModel.setStatus(MaterialStatusEnum.NORMAL.getStatus());
        materialExampleModel.setDelFlag(DeleteFlagEnum.NO.getCode());
        materialExampleModel.setUpdateBy(jwtUtils.getCurrentUserAccountId());
        materialExampleModel.setUpdateTime(new Date());
        materialExampleMapper.updateById(materialExampleModel);

        // 添加扩展字段信息
        if (!CollectionUtils.isEmpty(reqVo.getMaterialExampleExtVoList())) {
            // 删除历史记录
            Map<String, Object> columnMap = new HashMap<>();
            columnMap.put(MaterialExampleExtModel.MATERIAL_EXAMPLE_ID, reqVo.getId());
            materialExampleExtMapper.deleteByMap(columnMap);
            List<MaterialExampleExtReqVo> materialExampleExtVoList = reqVo.getMaterialExampleExtVoList();
            MaterialExampleExtModel materialExampleExtModel;
            for (MaterialExampleExtReqVo data : materialExampleExtVoList) {
                materialExampleExtModel = new MaterialExampleExtModel();
                materialExampleExtModel.setMaterialExampleId(materialExampleModel.getId());
                materialExampleExtModel.setFieldInfoId(data.getFieldInfoId());
                materialExampleExtMapper.insert(materialExampleExtModel);
            }
        }


    }

    @Override
    public MaterialExampleModel add(MaterialExampleReqVo reqVo) {
        MaterialExampleModel materialExampleModel = new MaterialExampleModel();
        BeanUtils.copyProperties(reqVo, materialExampleModel);
        materialExampleModel.setStatus(MaterialStatusEnum.NORMAL.getStatus());
        materialExampleModel.setDelFlag(DeleteFlagEnum.NO.getCode());
        materialExampleModel.setCreateBy(jwtUtils.getCurrentUserAccountId());
        materialExampleModel.setCreateTime(new Date());
        materialExampleMapper.insert(materialExampleModel);
        // 添加扩展字段信息
        if (!CollectionUtils.isEmpty(reqVo.getMaterialExampleExtVoList())) {
            List<MaterialExampleExtReqVo> materialExampleExtVoList = reqVo.getMaterialExampleExtVoList();
            MaterialExampleExtModel materialExampleExtModel;
            for (MaterialExampleExtReqVo data : materialExampleExtVoList) {
                materialExampleExtModel = new MaterialExampleExtModel();
                materialExampleExtModel.setMaterialExampleId(materialExampleModel.getId());
                materialExampleExtModel.setFieldInfoId(data.getFieldInfoId());
                materialExampleExtModel.setValue(data.getValue());
                materialExampleExtMapper.insert(materialExampleExtModel);
            }
        }
        return materialExampleModel;
    }

    @Override
    public GeneratePageListVo<MaterialExampleRespVo> getInventoryInsufficientPageList(Integer current, Integer size) {
        WarnConfigRespVo warnConfigRespVo = warnConfigService.getWarnConfig();
        if (!ObjectUtils.isEmpty(warnConfigRespVo)) {
            Integer configValue = warnConfigRespVo.getInventoryWarnValue();
            IPage<MaterialExampleRespVo> iPage = new Page<>(current, size);
            IPage<MaterialExampleRespVo> materialExampleRespVoIPage = materialExampleMapper.selectPageByCondition(iPage, null, null, null, configValue);
            return new GeneratePageListVo<>(materialExampleRespVoIPage);
        }
        return new GeneratePageListVo<>(new Page<>());
    }
}
